package cn.exrick.manager.service.impl;

import cn.exrick.common.pojo.DataTablesResult;
import cn.exrick.manager.mapper.TbAdviceMapper;
import cn.exrick.manager.pojo.TbAdvice;
import cn.exrick.manager.service.AdviceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdviceServiceImpl implements AdviceService {

    @Autowired
    private TbAdviceMapper tbAdviceMapper;

    @Override
    public void addAdvice(TbAdvice tbAdvice) {
        tbAdviceMapper.insert(tbAdvice);
    }

    @Override
    public void delAdvice(Long id) {
        tbAdviceMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updateAdvice(TbAdvice tbAdvice) {
        tbAdviceMapper.updateByPrimaryKeySelective(tbAdvice);
    }

    @Override
    public TbAdvice getAdviceById(Long id) {
        return tbAdviceMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<TbAdvice> getAdviceByMemeberId(Long memberId) {
        return tbAdviceMapper.getAdviceByMemeberId(memberId);
    }

    @Override
    public DataTablesResult getAdviceList(int draw, int start, int length, String username, String startTime, String endTime, String orderCol, String orderDir) {
        DataTablesResult result = new DataTablesResult();

        //分页执行查询返回结果
        PageHelper.startPage(start / length + 1, length);
        List<TbAdvice> list = tbAdviceMapper.selectAdviceByCondition(username, startTime, endTime, orderCol, orderDir);
        PageInfo<TbAdvice> pageInfo = new PageInfo<>(list);
        result.setRecordsFiltered((int) pageInfo.getTotal());
        result.setRecordsTotal((int) pageInfo.getTotal());

        result.setDraw(draw);
        result.setData(list);

        return result;
    }
}
